package view.welcome;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextField;
import controller.UserController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import view.Main;

public class WelcomeController {
    private Main myApp ;
    private static Alert alert = new Alert(Alert.AlertType.INFORMATION);
    private static String account;
    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public void setApp(Main myApp){
        this.myApp = myApp;
    }
    @FXML
    private JFXButton LoginButton;

    @FXML
    private JFXTextField loginAccount;

    @FXML
    private JFXButton RegisterButton;

    @FXML
    private JFXPasswordField loginPassword;

    @FXML
    private ImageView titleImage;

    @FXML
    private ImageView welcome;


    @FXML // This method is called by the FXMLLoader when initialization is complete
    void initialize() {
        assert  LoginButton!= null : "fx:id=\" LoginButton\" was not injected: check your FXML file 'Welcome.fxml'.";
        assert RegisterButton != null : "fx:id=\"RegisterButton\" was not injected: check your FXML file 'Welcome.fxml'.";
         titleImage.setImage(new Image("\\view\\welcome\\image\\welcome_title.JPEG",true));
         welcome.setImage(new Image("\\view\\welcome\\image\\welcome.png",true));
    }


    @FXML
    void toLoginPage(ActionEvent event) throws Exception {
        String string= UserController.getInstance().logIn(loginAccount.getText(),loginPassword.getText());
        switch (string) {
            case "该用户不存在":
            case "密码不正确":
                alert.setHeaderText("!");
                alert.setContentText(string);
                alert.showAndWait();
                break;
            case "admin":
                if(loginPassword.getText().equals("admin")){
                    myApp.goToAdminLoginPage();
                }else{
                    alert.setHeaderText("!");
                    alert.setContentText("密码不正确");
                    alert.showAndWait();
                }
                break;
            case "经销商":
                this.setAccount(loginAccount.getText());
                myApp.goToCustomerOrderPage();
                break;
            case "云工厂":
                this.setAccount(loginAccount.getText());
                myApp.goToCloudFactoryManagePage();


                break;
        }
    }

    @FXML
    void toRegisterPage(ActionEvent event) throws Exception {
        myApp.goToRegisterPage();
    }

}

